﻿using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;

namespace SharpDepend.WPF
{
    public class ClientManager : ISocketClientManager
    {
        internal class Client : ISocketClient
        {
            private TcpClient mClient;

            public bool IsConnected
            {
                get { return mClient != null && mClient.Connected; }
            }

            public Client()
            {
                mClient = new TcpClient();
                mClient.ReceiveBufferSize = 64;
                mClient.SendBufferSize = 64;
            }

            public Client(TcpClient client)
            {
                mClient = client;
                mClient.ReceiveBufferSize = 64;
                mClient.SendBufferSize = 64;
            }

            public void Connect(string ip, int port)
            {
                mClient.Connect(ip, port);
            }

            public void Write(byte[] data, int offset, int length)
            {
                mClient.GetStream().Write(data, offset, length);
            }

            public int GetAvailable()
            {
                return mClient.Available;
            }

            public int ReadByte()
            {
                return mClient.GetStream().ReadByte();
            }

            public void Read(byte[] data, int offset, int length)
            {
                mClient.GetStream().Read(data, offset, length);
            }

            public void Close()
            {
                mClient.Close();
            }

            public string IP
            {
                get
                {
                    return ((IPEndPoint)mClient.Client.RemoteEndPoint).Address.ToString();
                }
            }
        }

        public ISocketClient CreateClient()
        {
            return new Client();
        }
    }
}
